import React, {
    useEffect,
    useLayoutEffect,
    useState,
    useCallback
} from "react";
import {
    useDispatch
} from "react-redux";
import Main from "./main";
import axiosMethod from "../util/axios";
import $ from "jquery";
import {message, Spin} from "antd";
import {getQueryVariable,removeToken} from "../util";
import CONFIG from "../util/config";
import "./app.scss";
import { HashRouter as Router, Route, Switch } from 'react-router-dom';
import iframePage from "./iframePage";

const {
    originUrl,
    appAddr,
    imageAddr
} = CONFIG;

function App(props){
    const [init, setInit] = useState(false);
    const dispatch = useDispatch();
    const getBaseInfo = useCallback(()=>{  //获取系统信息和用户信息
        return new Promise((resolve, reject)=>{
            axiosMethod({
                type: "get",
                url: "/api/common/system/info",
                body: {},
                callback: function(res){
                    let baseUrl = res.baseUrl;
                    sessionStorage.setItem("baseUrl", baseUrl);
                    window._baseUrl = baseUrl;
                    dispatch({
                        type: "SET_SYSINFO",
                        data: res
                    })
                    let myToken = getQueryVariable("lg_tk")||sessionStorage.getItem("token")||localStorage.getItem("token");
                    if(!myToken){
                        message.warn("未获取到token");
                        window.location.href = sessionStorage.getItem("baseUrl") + `login.html?lg_sysid=260&lg_preurl=${encodeURIComponent(removeToken(window.location.href))}`
                        return;
                    }
                    let sign = false;
                    $("script").each((index, item)=>{
                        if($(item).attr("src") === baseUrl + "/UserMgr/Login/JS/CheckIsOnline2.js"){
                            sign = true;
                        }
                    })
                    if(!sign){
                        let script = document.createElement("script");
                        script.src = baseUrl + "/UserMgr/Login/JS/CheckIsOnline2.js";
                        script.type = "text/javascript";
                        script.onload = function(){
                            window._LgBase_initCheck(baseUrl, myToken, "260");
                        }
                        document.getElementsByTagName("head")[0].appendChild(script);
                    }
                    sessionStorage.setItem("token", myToken);
                    //获取用户信息
                    axiosMethod({
                        type: "get",
                        url: "/api/common/user",
                        body: {
                            token: myToken
                        },
                        callback: function(res){
                            if(!res){
                                message.warn("未获取到当前用户信息");
                                return;
                            }
                            //存储用户信息
                            dispatch({
                                type: "SET_USERINFO",
                                data: res
                            })
                            let identityCode = getQueryVariable("lg_ic");
                            let obj = res.identityList && res.identityList[0]? res.identityList[0]: {};
                            Array.isArray(res.identityList) &&
                            res.identityList.forEach((item)=>{
                                if(item.identityCode === identityCode){
                                    obj = item;
                                }
                            })
                            dispatch({
                                type: "SET_CURRENTIDENTITY",
                                data: obj
                            })
                            dispatch({
                                type: "SET_IDENTITYLIST",
                                data: res.identityList? res.identityList: []
                            })
                            resolve(res);
                        }
                    });
                }
            });
        })
       
    }, [dispatch]);
    const getCommonData = useCallback(async() => {
        let userInfo = await getBaseInfo();
        if(userInfo.userId){
            setInit(true);
        }
    }, [getBaseInfo]);
    useEffect(()=>{
        getCommonData();
    }, [getCommonData]);
    if(!init){
        return <Spin className="app_load" spinning={true} tip="加载中..." />
    }
    return (
        <Router>
            <Switch>
                <Route path={"/backups-reduction"} component={iframePage}></Route>
            </Switch>
        </Router>
    )
}

export default App;